package uz.efir.android.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsListener extends BroadcastReceiver {
	private static final String TAG = "SMS2HTTPPOST";

    @Override
    public void onReceive(Context context, Intent intent) {
    	Bundle extras = intent.getExtras();
    	if (extras == null) {
    		Log.e(TAG, "Got SMS_RECEIVED intent with null bundle extras");
    		return;
    	}
    	
    	Log.d(TAG, "Started processing the SMS...");

    	Object[] pdus = (Object[]) extras.get("pdus");
		for (int i = 0; i < pdus.length; i++) {
			Log.i(TAG, "Message no: " + (i + 1));
			SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String fromAddress = message.getOriginatingAddress();
            String messageBody = message.getMessageBody();
            
            Toast.makeText(context, "From: " + fromAddress
            		+ "\nMsg: " + messageBody, Toast.LENGTH_LONG);
        }
    }
}
